/*
* File     ui_small_prompt_page.c
* Description
*  Copyright (c) Hisilicon Technologies Co., Ltd. 2019-2019. All rights reserved.
* Author    HiMobileCam Reference Develop Team
* Date     2019/9/24
* Version  V1.0
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "ui_common.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /*  __cplusplus  */

typedef struct {
    HI_S32 powerOffTimerCnt;
}UI_PARK_PROMPT_Ctx;

static UI_PARK_PROMPT_Ctx g_uiParkPromptCtx = {.powerOffTimerCnt = 0,};
static const HI_U32 UI_POWER_OFF_TIMER_MAX = 10U;

HI_S32 PDT_UI_SmallPromptOnKeyDown(HIGV_HANDLE widgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    MLOGD("wParam: %#lx, lParam = %#lx\n", (HI_UL)wParam, (HI_UL)lParam);
    HI_S32 ret;

    switch (wParam) {
        case PDT_UI_KEY_BACK:
            /* fall-through */
        case PDT_UI_KEY_MENU:
            /* fall-through */
        case PDT_UI_KEY_OK:
            /* fall-through */
        case PDT_UI_KEY_EXPAND:
            ret = PDT_UI_WINMNG_FinishWindow(SMALL_PROMPT_WINDOW);
            if (ret != HI_SUCCESS) {
                MLOGE("finish alarm window failed\n");
                return ret;
            }
            break;

        default:
            MLOGW("key[0x%X]not support\n", wParam);
            break;
    }
    return HIGV_PROC_GOON;
}

HI_S32 PDT_UI_SmallPromptOnEvent(HIGV_HANDLE widgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    MLOGD("wParam: %#lx, lParam = %#lx\n", (HI_UL)wParam, (HI_UL)lParam);

    return HIGV_PROC_GOON;
}

HI_S32 PDT_UI_SmallPromptOnGestureTap(HIGV_HANDLE widgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    HI_S32 ret;

    ret = PDT_UI_WINMNG_FinishWindow(SMALL_PROMPT_WINDOW);
    HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_GOON, "PDT_UI_WINMNG_FinishWindow");

    return HIGV_PROC_GOON;
}

HI_S32 PDT_UI_SmallPromptOnTimer(HIGV_HANDLE widgetHdl, HI_PARAM wParam, HI_PARAM lParam)
{
    HI_S32 ret;
    HI_MESSAGE_S message;

    if (wParam == TIMER_REFRESH_ALARM_POWEROFF) {
        g_uiParkPromptCtx.powerOffTimerCnt++;

        if (g_uiParkPromptCtx.powerOffTimerCnt == UI_POWER_OFF_TIMER_MAX) {
            g_uiParkPromptCtx.powerOffTimerCnt = 0;

            ret = HI_GV_Timer_Stop(SMALL_PROMPT_WINDOW, TIMER_REFRESH_ALARM_POWEROFF);
            HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_STOP, "HI_GV_Timer_Stop");
            ret = HI_GV_Timer_Destroy(SMALL_PROMPT_WINDOW, TIMER_REFRESH_ALARM_POWEROFF);
            HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_STOP, "HI_GV_Timer_Destroy");

            message.what = HI_EVENT_STATEMNG_POWEROFF;
            ret = HI_PDT_STATEMNG_SendMessage(&message);
            HI_APPCOMM_CHECK_RETURN_WITH_ERRINFO(ret, HIGV_PROC_STOP, "Send poweroff msg");
            return HIGV_PROC_GOON;
        }

        ret = HI_GV_Timer_Reset(SMALL_PROMPT_WINDOW, TIMER_REFRESH_ALARM_POWEROFF);
        HI_APPCOMM_CHECK_RETURN(ret, ret);
    }else {
        MLOGW("timer[%x] not support!\n",wParam);
    }

    return HIGV_PROC_GOON;
}

HI_VOID PDT_UI_COMM_ShowSmallPrompt(HI_U32 stringID)
{
    HI_S32 ret;

    ret = PDT_UI_WINMNG_StartWindow(SMALL_PROMPT_WINDOW, HI_FALSE);
    if (ret) {
        MLOGE("Start Window failed. %x\n", ret);
        return;
    }

    ret = HI_GV_Widget_SetTextByID(SMALL_PROMPT_WINDOW_LABEL_INFO, stringID);
    if (ret) {
        MLOGE("SetTextByID failed. %x\n", ret);
    }

    ret = HI_GV_Widget_Paint(SMALL_PROMPT_WINDOW, 0);
    if (ret) {
        MLOGE("Paint ALARM_WINDOW failed. %x\n", ret);
    }
}


#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /*  __cplusplus  */

